package com.lancoo.ccas52.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.ccas52.aspect.LogAnnotation;
import com.lancoo.ccas52.entity.Student;
import com.lancoo.ccas52.entity.TeachingClass;
import com.lancoo.ccas52.entity.TeachingClassStudent;
import com.lancoo.ccas52.entity.TeachingClassTeacher;
import com.lancoo.ccas52.pojo.common.PageInfo;
import com.lancoo.ccas52.pojo.vo.TeachingClassCourseDto;
import com.lancoo.ccas52.pojo.vo.TeachingClassInput;
import com.lancoo.ccas52.pojo.vo.TeachingClassSegmentDto;
import com.lancoo.ccas52.service.PlanTeachingService;
import com.lancoo.ccas52.service.StudentService;
import com.lancoo.ccas52.service.TeachingClassService;
import com.lancoo.ccas52.util.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TeachingClassController
 * @Description ™
 * @Author quin
 * @Date 2023/1/31 14:02
 */
@Api(tags = "教学班相关")
@RestController
@RequestMapping("/api/v5.2.1/pc/teachingClass")
public class TeachingClassController {

    @Resource
    private TeachingClassService teachingClassService;
    @Resource
    private PlanTeachingService planTeachingService;
    @Resource
    private StudentService studentService;

    @ApiOperation("批量添加教学班")
    @PostMapping("/add")
    @LogAnnotation(operationType = "1", operationContent = "批量添加教学班")
    public ResponseObject<List<TeachingClass>> addTeachingClass(@ApiParam(value = "TeachingClassInput对象", required = true) @RequestBody List<TeachingClassInput> list) {
        return ResponseObject.ofSuccess(teachingClassService.addBatchTeachingClass(list, false));
    }

    @ApiOperation("批量删除教学班")
    @DeleteMapping("/remove")
    @LogAnnotation(operationType = "4", operationContent = "批量删除教学班")
    public ResponseObject<Integer> deleteTeachingClass(@ApiParam(value = "教学班id", required = true) @RequestBody List<Long> teachingClassIds) {
        return ResponseObject.ofSuccess(teachingClassService.deleteClass(teachingClassIds));
    }

    @ApiOperation("更新教学班")
    @PostMapping("/update")
    @LogAnnotation(operationType = "2", operationContent = "更新教学班")
    public ResponseObject<TeachingClass> updateTeachingClass(@ApiParam(value = "TeachingClassInput对象", required = true) @RequestBody TeachingClassInput teachingClassInput) {
        return ResponseObject.ofSuccess(teachingClassService.updateTeachingClass(teachingClassInput));
    }

    @ApiOperation("批量更新教学班基础信息-忽略老师学生")
    @PostMapping("/updateBatch")
    public ResponseObject<Boolean> updateBatchTeachingClass(@ApiParam(value = "TeachingClassInput对象", required = true) @RequestBody List<TeachingClassInput> list) {
        return ResponseObject.ofSuccess(teachingClassService.updateBatchTeachingClass(list));
    }

    @ApiOperation("判断教学班名是否存在")
    @GetMapping("/isExistTeachingClassName")
    public ResponseObject<Boolean> isExistTeachingClassName(@ApiParam(value = "学校Id", required = true) @RequestParam String schoolId,
                                                            @ApiParam(value = "学期", required = true) @RequestParam String term,
                                                            @ApiParam(value = "教学班id", required = false) @RequestParam(required = false) Long teachingClassId,
                                                            @ApiParam(value = "教学班名", required = true) @RequestParam String teachingClassName) {
        return ResponseObject.ofSuccess(teachingClassService.isExistTeachingClassName(schoolId, term, teachingClassId, teachingClassName));
    }

    @ApiOperation("确定教学班设置")
    @PostMapping("/ensureTeachingClass")
    public ResponseObject<List<TeachingClass>> ensureTeachingClass(@ApiParam(value = "学校Id", required = true) @RequestParam String schoolId,
                                                                   @ApiParam(value = "学期", required = true) @RequestParam String term,
                                                                   @ApiParam(value = "TeachingClassInput对象", required = true) @RequestBody List<TeachingClassInput> list) {
        return ResponseObject.ofSuccess(teachingClassService.ensureTeachingClass(schoolId, term, list));
    }

    @ApiOperation("获取本学期课程教学班数量")
    @GetMapping(value = "/getTeachingClassCount")
    public ResponseObject<Long> getTeachingClassNumByCourseId(@ApiParam(value = "学校Id", required = true) @RequestParam String schoolId,
                                                                             @ApiParam(value = "学期", required = true) @RequestParam String term,
                                                                             @ApiParam(value = "1课程  2环节", required = true) @RequestParam Integer flag,
                                                                             @ApiParam(value = "课程id||环节id", required = true) @RequestParam Long courseId) {
        return ResponseObject.ofSuccess(teachingClassService.getTeachingClassNumByCourseId(schoolId, term, flag, courseId));
    }

    @ApiOperation("合并教学班")
    @PutMapping("/combinedClass")
    public ResponseObject<Boolean> combinedClass(@ApiParam(value = "教学班id", required = true) @RequestBody List<Long> teachingClassIds) {
        return ResponseObject.ofSuccess(teachingClassService.combinedClass(teachingClassIds));
    }

    @ApiOperation("批量修改教师")
    @PostMapping("/updateTeacher")
    @LogAnnotation(operationType = "2", operationContent = "修改教学班老师")
    public ResponseObject<List<TeachingClassTeacher>> updateTeacher(@ApiParam(value = "教学计划id", required = true) @RequestParam Long teachingClassId,
                                                                    @ApiParam(value = "教师id", required = true) @RequestBody List<Long> teacherIds) {
        return ResponseObject.ofSuccess(teachingClassService.updateTeacher(teachingClassId, teacherIds));
    }

    @ApiOperation("批量修改学生")
    @PostMapping("/updateStudent")
    @LogAnnotation(operationType = "2", operationContent = "批量修改教学班学生")
    public ResponseObject<List<TeachingClassStudent>> updateStudent(@ApiParam(value = "教学计划id", required = true) @RequestParam Long teachingClassId,
                                                                    @ApiParam(value = "学生id", required = true) @RequestBody List<Long> studentIds) {
        return ResponseObject.ofSuccess(teachingClassService.updateStudent(teachingClassId, studentIds));
    }

    @ApiOperation("根据课程id获取教学班列表")
    @GetMapping(value = "/getByCourse")
    public ResponseObject<List<TeachingClass>> getTeacherPlan(@ApiParam(value = "1课程  2环节", required = true) @RequestParam Integer flag,
                                                              @ApiParam(value = "课程id||环节id", required = true) @RequestParam Long courseId,
                                                              @ApiParam(value = "修读性质：1主修2辅修", required = true) @RequestParam Integer studyNature,
                                                              @ApiParam(value = "0无需选课 1在线选课;2:分级教学 3:预选教学班", required = true) @RequestParam Integer teachingClassType) {
        return ResponseObject.ofSuccess(teachingClassService.getByCourseIdAndStudyNature(flag, courseId, studyNature, teachingClassType));
    }


    @ApiOperation("根据任务获取课程教学班详情列表")
    @GetMapping(value = "/getByTask")
    public ResponseObject<PageInfo<TeachingClassCourseDto>> getCourseAndApply(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                              @ApiParam(value = "0已取消 1正常开班-（发布后使用？） 2未设置-待处理 3或不传全部", required = false) @RequestParam(required = false) Integer handleState,
                                                                              @ApiParam(value = "选课状态，0：全部，1：开课条件以下，2：达到开课条件", required = false) @RequestParam(required = false) Integer selectState,
                                                                              @ApiParam(value = "课程类别id 传null全部", required = false) @RequestParam(required = false) String courseTypeId,
                                                                              @ApiParam(value = "修读性质 1主 2辅修", required = false) @RequestParam(required = false) Integer studyNature,
                                                                              @ApiParam(value = "课程性质 0全部 1必修  2选修", required = false) @RequestParam(required = false) Integer courseNature,
                                                                              @ApiParam(value = "专业性质，1:公共课，2:专业课", required = false) @RequestParam(required = false) Integer majorProp,
                                                                              @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                                              @ApiParam(value = "课程名模糊查询", required = false) @RequestParam(required = false) String keyWord,
                                                                              @ApiParam(value = "0无需选课 1在线选课;2:分级教学 3:预选教学班 4:除预选外教学班", required = true) @RequestParam Integer teachingClassType,
                                                                              @ApiParam(value = "不传显示全部 最低开班人数：未达到开班人数 0达到开班条件", required = false) @RequestParam(required = false) Integer minClassNum,
                                                                              @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                              @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<TeachingClassCourseDto> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
        return ResponseObject.ofSuccess(teachingClassService.getTeachingClassDtoByConditions(page, taskId, handleState, selectState, courseTypeId, studyNature, courseNature, majorProp, collegeId, keyWord, teachingClassType, minClassNum));
    }

    @ApiOperation("确定选课结果-在线选课教学班列表")
    @GetMapping(value = "/getSelectionTeachingClass")
    public ResponseObject<PageInfo<TeachingClassCourseDto>> getSelectionTeachingClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                              @ApiParam(value = "0已取消 1正常开班-（发布后使用？） 2未设置-待处理 3或不传全部", required = false) @RequestParam(required = false) Integer handleState,
                                                                              @ApiParam(value = "选课状态，0：全部，1：开课条件以下，2：达到开课条件", required = false) @RequestParam(required = false) Integer selectState,
                                                                              @ApiParam(value = "课程类别id 传null全部", required = false) @RequestParam(required = false) String courseTypeId,
                                                                              @ApiParam(value = "修读性质 1主 2辅修", required = false) @RequestParam(required = false) Integer studyNature,
                                                                              @ApiParam(value = "课程性质 0全部 1必修  2选修", required = false) @RequestParam(required = false) Integer courseNature,
                                                                              @ApiParam(value = "专业性质，1:公共课，2:专业课", required = false) @RequestParam(required = false) Integer majorProp,
                                                                              @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                                              @ApiParam(value = "课程名模糊查询", required = false) @RequestParam(required = false) String keyWord,
                                                                              @ApiParam(value = "最低开班人数", required = false) @RequestParam(required = false,defaultValue = "30") Integer minClassNum,
                                                                              @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                              @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<TeachingClassCourseDto> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
        return ResponseObject.ofSuccess(teachingClassService.getSelectionTeachingClass(page, taskId, handleState, selectState, courseTypeId, studyNature, courseNature, majorProp, collegeId, keyWord, minClassNum));
    }

    @ApiOperation("根据任务获取环节教学班详情列表")
    @GetMapping(value = "/getSegmentClassByTask")
    public ResponseObject<PageInfo<TeachingClassSegmentDto>> getSegmentClassByTask(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                                   @ApiParam(value = "课程类别id 传null全部", required = false) @RequestParam(required = false) String segmentTypeId,
                                                                                   @ApiParam(value = "修读性质 1主 2辅修", required = false) @RequestParam(required = false) Integer studyNature,
                                                                                   @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                                                   @ApiParam(value = "课程名模糊查询", required = false) @RequestParam(required = false) String keyWord,
                                                                                   @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                                   @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<TeachingClassSegmentDto> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
        return ResponseObject.ofSuccess(teachingClassService.getTeachingClassSegment(page, taskId, segmentTypeId, studyNature, collegeId, keyWord));
    }

    @ApiOperation("批量修改教学班状态")
    @PostMapping(value = "/withdraw")
    public ResponseObject<Object> updateTeacherPlan(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                    @ApiParam(value = "教学班id,逗号分割", required = true) @RequestParam String teachingClassIds,
                                                    @ApiParam(value = "0不可用-取消班级 1可用-正常开班 2未设置-撤回", required = true) @RequestParam Integer isEnable) {
        if (StrUtil.isBlank(teachingClassIds)) {
            return ResponseObject.ofFail("教学班id不能为空");
        }
        String[] split = teachingClassIds.split(",");
        List<Long> teachingClassIdList = new ArrayList<>();
        for (String s : split) {
            teachingClassIdList.add(Long.parseLong(s));
        }
        teachingClassService.updateTeachingClassEnable(teachingClassIdList, isEnable);
        return ResponseObject.ofSuccess("修改成功");
    }

    @ApiOperation("根据教学班id获取学生列表")
    @GetMapping(value = "/getStudentsByTeachingClassId")
    public ResponseObject<PageInfo<Student>> getStudentsByTeachingClassId(@ApiParam(value = "教学班id", required = true) @RequestParam Long teachingClassId,
                                                                          @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                          @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<Student> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
        return ResponseObject.ofSuccess(studentService.getStudentsByTeachingClassId(page, teachingClassId));
    }

    @ApiOperation("根据课程id,修读性质获取已选学生列表")
    @GetMapping(value = "/getStudentsCourse")
    public ResponseObject<PageInfo<Student>> getStudentsCourse(@ApiParam(value = "1课程  2环节", required = true) @RequestParam Integer flag,
                                                               @ApiParam(value = "课程id||环节id", required = true) @RequestParam Long courseId,
                                                               @ApiParam(value = "修读性质：1主修2辅修", required = true) @RequestParam Integer studyNature,
                                                               @ApiParam(value = "0无需选课 1在线选课;2:分级教学 3:预选教学班", required = true) @RequestParam Integer teachingClassType,
                                                               @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                               @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<Student> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
        return ResponseObject.ofSuccess(studentService.getStudentsCourse(page, flag, courseId, studyNature, teachingClassType));
    }

    @ApiOperation("生成教学班")
    @PostMapping(value = "/generationTeachingClass")
    public ResponseObject<List<TeachingClass>> generationTeachingClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                       @ApiParam(value = "课程/环节id", required = true) @RequestParam Long courseId,
                                                                       @ApiParam(value = "修读性质：1主修2辅修", required = true) @RequestParam Integer studyNature,
                                                                       @ApiParam(value = "需生成教学班数量", required = true) @RequestParam Integer teachingClassNum,
                                                                       @ApiParam(value = "0无需选课 1在线选课;2:分级教学 3:预选教学班", required = true) @RequestParam Integer teachingClassType,
                                                                       @ApiParam(value = "校区id", required = true) @RequestParam String campusId,
                                                                       @ApiParam(value = "校区名", required = true) @RequestParam String campusName,
                                                                       @ApiParam(value = "教学班容量", required = false) @RequestParam(required = false) Integer classNum,
                                                                       @ApiParam(value = "已存在教学班数量", required = false) @RequestParam(required = false, defaultValue = "0") Integer headPointer,
                                                                       @ApiParam(value = "1课程  2环节", required = true) @RequestParam Integer flag) {
        return ResponseObject.ofSuccess(planTeachingService.generationTeachingClass(taskId, courseId, studyNature, teachingClassNum, teachingClassType, classNum, flag, campusId, campusName, headPointer));
    }

    @ApiOperation("快速平均分配学生生成")
    @PostMapping(value = "/generationStudent")
    public ResponseObject<Integer> generationStudent(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                     @ApiParam(value = "课程/环节id", required = true) @RequestParam Long courseId,
                                                     @ApiParam(value = "修读性质：1主修2辅修", required = true) @RequestParam Integer studyNature,
                                                     @ApiParam(value = "1课程  2环节", required = true) @RequestParam Integer flag,
                                                     @ApiParam(value = "校区id", required = true) @RequestParam String campusId,
                                                     @ApiParam(value = "教学班id", required = true) @RequestBody List<Long> teachingClassIds) {
        return ResponseObject.ofSuccess(planTeachingService.generationStudent(courseId, studyNature, flag, teachingClassIds, campusId));
    }

    @ApiOperation("生成教学班容量")
    @PostMapping(value = "/generationClassNum")
    public ResponseObject<Integer> generationClassNum(@ApiParam(value = "参考人数", required = true) @RequestParam Integer studentNum,
                                                      @ApiParam(value = "教学班id", required = true) @RequestBody List<Long> teachingClassIds) {
        return ResponseObject.ofSuccess(planTeachingService.generationClassNum(teachingClassIds, studentNum));
    }


}
